package com.kindergarten.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kindergarten.annotaion.KinderGartenLogger;
import com.kindergarten.pojo.*;
import com.kindergarten.service.IPayService;
import com.kindergarten.service.IUserService;
import com.kindergarten.utils.DataResults;
import com.kindergarten.utils.MMGridPageVoBean;
import com.kindergarten.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author bruce
 * @since 2022-10-27
 */
@Controller
@RequestMapping("/pay-api")
public class PayController {

    @Autowired
    IPayService payService;

    @Autowired
    IUserService iUserService;

    /**
     * 01-跳转到模板引擎页面
     * 只有具备这个角色的用户，才能操作这个控制器方法，否则403
     * @return
     */
    @Secured(value = {"ROLE_超级管理员"})
    @GetMapping("topage")
    @KinderGartenLogger(logDescription = "薪资模块-跳转薪资页面",methodType = "java.lang.String")
    public String topage() {
        return "pay";
    }


    /**
     * 02-跳转到模板引擎页面
     *
     * @return
     */
    @GetMapping("toaddpage")
    @KinderGartenLogger(logDescription = "薪资模块-跳转新增薪资页面",methodType = "java.lang.String")
    public String toaddpage(Model model) {
        List<User> userList = iUserService.list(new QueryWrapper<User>().eq("del", 0).eq("user_state", "2"));
        model.addAttribute("userList", userList);
        return "payAdd";
    }

    /**
     * 02-数据加载分页
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("page")
    @KinderGartenLogger(logDescription = "薪资模块-薪资数据加载",methodType = "com.kindergarten.utils.MMGridPageVoBean")
    public MMGridPageVoBean page(Pay pay) {

        MMGridPageVoBean pageVoBean = new MMGridPageVoBean();
        QueryWrapper<Pay> queryWrapper = new QueryWrapper<Pay>();
        queryWrapper.orderByDesc("id");
        queryWrapper.eq("del", 0); //删除不查询

        queryWrapper.ge(pay.getMinBase() != null, "base", pay.getMinBase());
        queryWrapper.le(pay.getMaxBase() != null, "base", pay.getMaxBase());
        queryWrapper.like(pay.getMinDate() != null, "payment_time", pay.getMinDate());

        IPage<Pay> page = payService.page(new Page<Pay>(pay.getPage(), pay.getLimit()), queryWrapper);
        List<Pay> records = page.getRecords();
        for (Pay record : records) {
            String userId = record.getName();
            String userName = iUserService.getById(userId).getUserName();
            record.setUserName(userName);
        }

        pageVoBean.setRows(records); //每页数据
        pageVoBean.setTotal(page.getTotal()); //总条数
        return pageVoBean;
    }

    /**
     * 新增
     *
     * @return
     */
    @ResponseBody
    @PostMapping("add")
    @KinderGartenLogger(logDescription = "薪资模块-新增薪资数据",methodType="com.kindergarten.utils.DataResults")
    public DataResults add(Pay pay) {
        return DataResults.success(ResultCode.SUCCESS, payService.save(pay));
    }

    /**
     * 02-跳转到模板引擎页面
     *
     * @return
     */
    @GetMapping("toupdatepage")
    @KinderGartenLogger(logDescription = "薪资模块-跳转更新页面")
    public String toupdatepage(Integer id, Model model) {
        Pay pay = payService.getById(id);
        model.addAttribute("pay", pay);
        List<User> userList = iUserService.list(new QueryWrapper<User>().eq("del", 0).eq("user_state", "2"));
        model.addAttribute("userList", userList);
        return "payUpdate";  //模板引擎页面
    }

    /**
     * 更新
     *
     * @return
     */
    @ResponseBody
    @PutMapping("update")
    @KinderGartenLogger(logDescription = "薪资模块-更新薪资数据",methodType="com.kindergarten.utils.DataResults")
    public DataResults update(Pay pay) {
        return DataResults.success(ResultCode.SUCCESS, payService.updateById(pay));
    }

    /**
     * 删除
     *
     * @return
     */
    @ResponseBody
    @DeleteMapping("delete/{id}")
    @KinderGartenLogger(logDescription = "薪资模块-删除薪资数据",methodType="com.kindergarten.utils.DataResults")
    public DataResults delete(@PathVariable("id") Integer id) {
        Pay pay = new Pay();
        pay.setId(id);
        pay.setDel(1);
        return DataResults.success(ResultCode.SUCCESS, payService.updateById(pay));
    }


}
